package com.saui.traceu.controller;

import java.util.ArrayList;
import java.util.List;

import com.saui.traceu.model.Session;
import com.saui.traceu.model.TraceDBAdapter;

/**
 * A class for coordinating Session information for current session and displayed session.
 * 
 * @author Tim Andrianoff
 *
 */

public class SessionManager {
	private List<Session> activeSessions;
	private Session currentSession = null;
	
	private static SessionManager instance;
	
	/**
	 * Use this to get the instance of SessionManager
	 * 
	 * @return the singleton instance of the session manager
	 */
	public static SessionManager getSessionManager() {
		if (instance == null) {
			instance = new SessionManager();
		} 
		return instance;
	}
	
	private SessionManager() {
		activeSessions = new ArrayList<Session>();
	}
	
	// Manage displayed/selected sessions
	
	/**
	 * 
	 * @param activeSessions
	 */
	public void setActiveSessions(List<Session> activeSessions) {
		activeSessions = activeSessions;
	}
	

	public List<Session> getActiveSessions() {
		return activeSessions;
	}
	
	
	// Current session management
	
	/**
	 * handle starting a new recording session.
	 * 
	 * @param a TraceDBAdapter
	 */
	public void startNewSession(TraceDBAdapter mDb) {
		currentSession = new Session();
		long id = mDb.createSession(currentSession);
		currentSession.setId(id);
	}
	
	public void pauseCurrentSession() {
		
	}
	
	public void saveAndEndCurrentSession(TraceDBAdapter mDb) {
		currentSession.endSession();
		currentSession.setSaved(true);
		mDb.updateSession(currentSession);
	}
	
	public void endCurrentSession() {
		currentSession = null;
	}
	
	public void renameCurrentSession(String name) {
		currentSession.setTitle(name);
	}
	
	public Session getCurrentSession() {
		return currentSession;
	}
	
	public void setCurrentSession(Session currentSession) {
		this.currentSession = currentSession;
	}
}
